

const util = require('../../utils/util.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showpopup: false,
    storesItem: {},
    storesItemValue: '',
    storeList: [],

    username: '',
    mobile: '',
    hospitalName: '',
    serviceTimeBeg: '',
    serviceTimeEnd: '',
    situationDescription: '',
    remark: '',

    dateType: 1,
    serviceTimeBeg2: 0,
    serviceTimeEnd2: 0,
    showpopup2: false,
    minHour: 0,
    maxHour: 24,
    minDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    currentDate: new Date().getTime()


  },
  onDateCancel() {
    this.setData({
      showpopup2: false
    })
  },
  onDateConfirm(date) {
    let s = this.formatDate(date.detail)
    const type = this.data.dateType;
    if (type === 1) {
      this.setData({
        showpopup2: false,
        serviceTimeBeg2: date.detail,
        serviceTimeBeg: s
      })
    } else {
      this.setData({
        showpopup2: false,
        serviceTimeEnd2: date.detail,
        serviceTimeEnd: s
      })
    }
  },
  doDate() {
    this.setData({
      dateType: 1,
      showpopup2: true
    })
  },
  doDate2() {
    this.setData({
      dateType: 2,
      showpopup2: true
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}  ${date.getHours()}:${date.getMinutes()}`;
  },
  // 
  onPopupClick() {
    this.setData({
      showpopup: true,
    })
  },
  onPopupClose() {
    this.setData({
      showpopup: false,
    })
  },
  doPopupItem(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      storesItem: data.item,
      showpopup: false,
      storesItemValue: data.item.address + '，' + data.item.name
    })
  },
  getStoresInit() {
    util.https('storesMgmt/storesQry', {
    }, (res) => {
      let storesBoList = res.storesBoList;
      this.setData({
        storeList: storesBoList
      })
    }, (res) => {
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    })
  },
  doSubmit() {
    let sData = this.data;
    console.log(sData)
    let postData = {
      name: sData.username,
      phone: sData.mobile,
      serviceTimeBeg: sData.serviceTimeBeg,
      serviceTimeEnd: sData.serviceTimeEnd,
      situationDescription: sData.situationDescription,
      hospitalName: sData.hospitalName,
      remark: sData.remark,
      "storesId": sData.storesItem.id,
      "storesName": sData.storesItem.name,
      "storesAddress": sData.storesItem.address,
    };
    if (postData.name == '' || postData.name == null) {
      wx.showModal({
        title: '温馨提示',
        content: '姓名不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (!/^1[3456789]\d{9}$/.test(postData.phone)) {
      console.log('电话号码不正确！')
      wx.showModal({
        title: '温馨提示',
        content: '电话号码不正确！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.hospitalName == '' || postData.hospitalName == null) {
      wx.showModal({
        title: '温馨提示',
        content: '就诊医院不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (!postData.serviceTimeEnd || !postData.serviceTimeBeg) {
      wx.showModal({
        title: '温馨提示',
        content: '陪诊时间不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (postData.serviceTimeEnd <= postData.serviceTimeBeg) {
      wx.showModal({
        title: '温馨提示',
        content: '陪诊结束时间必须大于开始时间！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }

    // if (!/^(0|[1-9]|[1-9]\d|1\d\d|2[0-4])$/.test(postData.serviceTimeBeg) || !/^(0|[1-9]|[1-9]\d|1\d\d|2[0-4])$/.test(postData.serviceTimeEnd) || parseFloat(postData.serviceTimeBeg) >= parseFloat(postData.serviceTimeEnd)) {
    //   wx.showModal({
    //     title: '温馨提示',
    //     content: '陪诊时间不正确',
    //     confirmColor: '#F08302',
    //     showCancel: false,
    //     success(res) {
    //     }
    //   })
    //   return
    // }
    if (postData.storesId == '' || postData.storesId == null) {
      wx.showModal({
        title: '温馨提示',
        content: '门店不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    console.log(postData)
    // return
    util.https('medicalChaperon/medicalChaperonAdd', postData, (res) => {
      console.log(res)
      wx.redirectTo({
        url: '/page2/result/index',
      })
    }, (res) => {
      console.log(res)
      wx.showModal({
        title: '温馨提示',
        content: res.errorMsg,
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let currentDate = new Date(), currentDate2 = new Date();
    const year = currentDate.getFullYear();
    currentDate2.setFullYear(year + 3);
    this.setData({
      maxDate: currentDate2.getTime()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStoresInit();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */

})